import time
import numpy as np
from mcom import mcom
threejs_bridge = mcom(ip='127.0.0.1', port=12084, path='RECYCLE/v2d_logger/',
                       digit=8, rapid_flush=False, draw_mode='Threejs')

#################exp2############################
# 3tw = 2pi, t=2pi/3w=2*180
threejs_bridge.v2d_init()
threejs_bridge.set_style('nogrid')
threejs_bridge.set_style('star')
n_row = 10
n_col = 10
for t in range(60):
    for i in range(n_row):
        for j in range(n_col):
            omega = np.pi / 90
            theta2 = 3*t*omega + np.sqrt(i**2/50 + j**2/50)*4
            pos3 = np.array([i, j, np.sin(theta2)])
            id = j+n_col*i
            threejs_bridge.v2dx(
                'ball|%d|%s|%.2f'%(id, 'r', 0.1*(np.sin(theta2)+1.5) ),
                pos3[0],
                pos3[1],
                pos3[2],
                vel_dir=t,
                health=0,
                label='')
    threejs_bridge.flash('beam', src=t, dst=t+11, dur=2.0, size=0.1)
    # threejs_bridge.flash('lightning', src=t, dst=t+11, dur=2.0, size=0.05)


    # for i in range(n_obj):
    #     d_theta = 2*np.pi/n_obj
    #     omega = np.pi / 180
    #     theta = (d_theta * i) + t*omega
    #     theta2 = 3*(d_theta * i) + 3*t*omega
    #     pos3 = np.array([np.cos(theta)*10, np.sin(theta)*10, np.sin(theta2)*3])
    #     threejs_bridge.v2dx(
    #         'tank|%d|%s|%.2f'%(i, 'g', np.abs(np.sin(theta2)) ),
    #         pos3[0],
    #         pos3[1],
    #         pos3[2],
    #         vel_dir=t,
    #         health=0,
    #         label='')

    threejs_bridge.v2d_show()
print('all done')
time.sleep(1e5)










###############exp1#######################
threejs_bridge.v2d_init()

for t in range(2000):
    n_obj = 50
    for i in range(n_obj):
        d_theta = 2*np.pi/n_obj
        omega = np.pi / 180
        theta = (d_theta * i) + t*omega
        theta2 = 3*(d_theta * i) + 3*t*omega
        pos3 = np.array([np.cos(theta)*10, np.sin(theta)*10, np.sin(theta2)*3])
        threejs_bridge.v2dx(
            'tank|%d|%s|%.2f'%(i, 'g', np.abs(np.sin(theta2)) ),
            pos3[0],
            pos3[1],
            pos3[2],
            vel_dir=t,
            health=0,
            label='')
        threejs_bridge.v2dx(
            'tank|%d|%s|%.2f'%(i+n_obj, 'g', np.abs(np.sin(theta2))),
            pos3[0],
            pos3[1],
            -pos3[2],
            vel_dir=t,
            health=0,
            label='')

    threejs_bridge.v2d_show()
print('all done')
time.sleep(1e5)
